﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RTUParameterConfig.Class;

namespace RTUParameterConfig
{
    public partial class FlowmeterSetting : Form
    {
        public int intIndex;

        public FlowmeterSetting(int index)
        {
            InitializeComponent();
            intIndex = index;

            this.cbb1.Properties.Items.AddRange(Common.WaterFlowShowType);
            this.cbb1.SelectedIndex = 0;
            this.cbb2.Properties.Items.AddRange(Common.WaterFlowAccuracy);
            this.cbb2.SelectedIndex = 0;
            this.cbb3.Properties.Items.AddRange(Common.OpenStatus);
            this.cbb3.SelectedIndex = 0;
            this.cbb4.Properties.Items.AddRange(Common.OpenStatus);
            this.cbb4.SelectedIndex = 0;
            SetValue();
        }

        /// <summary>
        /// 设置水位计配置
        /// </summary>
        public void SetValue()
        {
            try
            {
                if (intIndex == 0)
                {
                    nud1.Value = Convert.ToDecimal(BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.WaterFlowCorrection1.Reverse<byte>().ToArray(), 0) / 1000.0);
                    cbb1.SelectedIndex = ParameterConfig.rtuConfigInfo.WaterFlowShowType[0];
                    cbb2.SelectedIndex = ParameterConfig.rtuConfigInfo.WaterFlowAccuracy[0];
                    cbb3.SelectedIndex = ParameterConfig.rtuConfigInfo.WaterFlowCumulativeSwitch[0];
                    //nud2.Value = Convert.ToDecimal(BitConverter.ToUInt32(ParameterConfig.rtuConfigInfo.WaterFlowTotal1.Reverse<byte>().ToArray(), 0) / 100000.0);
                    cbb4.SelectedIndex = ParameterConfig.rtuConfigInfo.WaterFlowTwoway[0];
                    nud3.Value = ParameterConfig.rtuConfigInfo.WaterFlowWaveFiltering[0];
                }

                if (intIndex == 1)
                {
                    nud1.Value = Convert.ToDecimal(BitConverter.ToUInt16(ParameterConfig.rtuConfigInfo.WaterFlowCorrection2.Reverse<byte>().ToArray(), 0) / 1000.0);
                    cbb1.SelectedIndex = ParameterConfig.rtuConfigInfo.WaterFlowShowType[1];
                    cbb2.SelectedIndex = ParameterConfig.rtuConfigInfo.WaterFlowAccuracy[1];
                    cbb3.SelectedIndex = ParameterConfig.rtuConfigInfo.WaterFlowCumulativeSwitch[1];
                    //nud2.Value = Convert.ToDecimal(BitConverter.ToUInt32(ParameterConfig.rtuConfigInfo.WaterFlowTotal2.Reverse<byte>().ToArray(), 0) / 100000.0);
                    cbb4.SelectedIndex = ParameterConfig.rtuConfigInfo.WaterFlowTwoway[1];
                    nud3.Value = ParameterConfig.rtuConfigInfo.WaterFlowWaveFiltering[1];
                }
            }
            catch
            {
                Common.ShowErrorMessage("加载高级设置参数失败！");
            }
}

        /// <summary>
        /// 获取水位计配置
        /// </summary>
        public void GetValue()
        {
            if (intIndex == 0)
            {
                ParameterConfig.rtuConfigInfo.WaterFlowCorrection1 = BitConverter.GetBytes(Convert.ToUInt16(nud1.Value * 1000)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.WaterFlowShowType[0] = Convert.ToByte(cbb1.SelectedIndex);
                ParameterConfig.rtuConfigInfo.WaterFlowAccuracy[0] = Convert.ToByte(cbb2.SelectedIndex);
                ParameterConfig.rtuConfigInfo.WaterFlowCumulativeSwitch[0] = Convert.ToByte(cbb3.SelectedIndex);
                //ParameterConfig.rtuConfigInfo.WaterFlowTotal1 = BitConverter.GetBytes(Convert.ToUInt32(nud2.Value * 100000)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.WaterFlowTwoway[0] = Convert.ToByte(cbb4.SelectedIndex);
                ParameterConfig.rtuConfigInfo.WaterFlowWaveFiltering[0] = Convert.ToByte(nud3.Value);
            }

            if (intIndex == 1)
            {
                ParameterConfig.rtuConfigInfo.WaterFlowCorrection2 = BitConverter.GetBytes(Convert.ToUInt16(nud1.Value * 1000)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.WaterFlowShowType[1] = Convert.ToByte(cbb1.SelectedIndex);
                ParameterConfig.rtuConfigInfo.WaterFlowAccuracy[1] = Convert.ToByte(cbb2.SelectedIndex);
                ParameterConfig.rtuConfigInfo.WaterFlowCumulativeSwitch[1] = Convert.ToByte(cbb3.SelectedIndex);
                //ParameterConfig.rtuConfigInfo.WaterFlowTotal2 = BitConverter.GetBytes(Convert.ToUInt32(nud2.Value * 100000)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.WaterFlowTwoway[1] = Convert.ToByte(cbb4.SelectedIndex);
                ParameterConfig.rtuConfigInfo.WaterFlowWaveFiltering[1] = Convert.ToByte(nud3.Value);
            }
        }

        /// <summary>
        /// 关闭前保存修改的配置信息
        /// </summary>
        private void WaterLevelSetting_FormClosing(object sender, FormClosingEventArgs e)
        {
            GetValue();
            this.Dispose();
        }
    }
}
